window.onload = function(){
	//定一个共用的前缀
	var api = 'http://h6.duchengjiu.top/shop/';
	//拿到元素
	var userName = document.getElementById('username');
	var passWord = document.getElementById('password');
	var nameMsg = document.getElementById('name-msg');
	var psdMsg = document.getElementById('psd-msg');
	var loginBtn = document.getElementById('login');
	var oModal = document.getElementById('modal');
	var oNeibu = document.getElementById('neibu');
	
	//点击登录按钮
	loginBtn.onclick = function(){
		var userNameV = userName.value;
		var passWordV = passWord.value;
		//请求登录
		$.post(api + 'api_user.php', {'status':'login', 'username':userNameV, 'password':passWordV}, function(result){
			console.log(result);
			if(result.code == 2002){
				nameMsg.innerHTML = '用户名不存在';
				nameMsg.style.color = 'red';
			}else if(result.code == 1001){
				psdMsg.innerHTML = '密码错误';
				psdMsg.style.color = 'red';
			}else if(result.code == 0){
				//一旦登录成功之后，去设置一个本地储存
				nameMsg.innerHTML = '';
				psdMsg.innerHTML = '';
				localStorage.setItem('token', result.data.token);
				localStorage.setItem('username', result.data.username);
				
				//先跳首页
				//alert('登录成功，点击确定跳转到首页');
				var n = 3;
				oModal.style.display = 'block';
				oNeibu.innerHTML = '登录成功，<span>'+n+'</span>秒之后跳转';
				//定时器做下倒计时
				timer = setInterval(function(){
					//倒计时
					n--;
					if(n == 0){
						clearInterval(timer);
						var goodsId = getUrlValue('goods_id');
						if(goodsId){
							location.href='xiangqing.html?goods_id='+goodsId;
						}else{
							location.href = 'index.html';
						}
					}
					oNeibu.innerHTML = '登录成功，<span>'+n+'</span>秒之后跳转';
				}, 1000);
				
			}
		})
	};
	
};



//console.log(window.location)
//这个方法用来干 什么的？
function getUrlValue(name){//?goods_id=257557&age=haha
		var str = location.search.substr(1);//goods_id=257557
		var reg = new RegExp("(^|&)"+name+"=([^&]*)(&|$)");
		var result = str.match(reg);
		//console.log(result);
		if(result === null) return null;
		return result[2];
};